package com.yhl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yhl.entity.Role;
import com.yhl.entity.User;
import com.yhl.mapper.RoleMapper;
import com.yhl.service.RoleService;
import com.yhl.service.UserService;
import lombok.val;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    HttpSession session;


    @Autowired
    RoleService roleService;

    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("/login.do")
    public String login(String userCode,String userPassword){
        var userCodeDB = userService.getUserByUserCode(userCode);
        if(userCodeDB!=null){
            if(userPassword.equals(userCodeDB.getUserPassword())){
                session.setAttribute("userSession", userCodeDB);
                redisTemplate.opsForValue().set("userSession",userCodeDB);
                return "frame";
            }
        }
        return "login";
    }

    @RequestMapping("/logout.do")
    public String logout(){
        session.invalidate();
        redisTemplate.delete("userSession");
        return "login";
    }

    @RequestMapping("/jsp/validatePwd.do")
    @ResponseBody
    public HashMap validatePwd(String oldpassword){
        User user = (User) redisTemplate.opsForValue().get("userSession");
        HashMap hashMap = new HashMap();
        if(oldpassword==null||oldpassword.equals("")){
            hashMap.put("result","error");
        }else if(oldpassword.equals(user.getUserPassword())){
            hashMap.put("result","true");
        }else{
            hashMap.put("result","false");
        }
        return hashMap;
    }

    @RequestMapping("/updPwd.do")
    public String updPwd(String rnewpassword){
        User user = (User)redisTemplate.opsForValue().get("userSession");
        user.setUserPassword(rnewpassword);
        userService.updateUser(user);
        return "redirect:logout.do";
    }


    @RequestMapping("/userlist.do")
    public String userList(String pageIndex,String queryname,String queryUserRole){
        User user = new User();
        user.setUserName(queryname);
        queryUserRole=queryUserRole==null?"0":queryUserRole;
        user.setUserRole(Integer.parseInt(queryUserRole));
        pageIndex=pageIndex==null?"1":pageIndex;
        user.setCurrentPage(Integer.valueOf(pageIndex));
        IPage<User> userIPage = userService.getUserByPage(user);

        List<Role> roleList = roleService.getRoleList();
        session.setAttribute("roleList",roleList);
        session.setAttribute("userList",userIPage.getRecords());
        session.setAttribute("queryUserName",queryname);
        session.setAttribute("queryUserRole",Integer.valueOf(queryUserRole));
        session.setAttribute("totalPageCount",userIPage.getPages());
        session.setAttribute("totalCount",userIPage.getTotal());
        session.setAttribute("currentPageNo",userIPage.getCurrent());
        return "userlist";
    }


    @RequestMapping("/jsp/userexist.do")
    @ResponseBody
    public HashMap<String,String> userexist(String userCode){
        HashMap<String,String> resultMap = new HashMap<>();
        System.out.println(userCode);
        User user = userService.getUserByUserCode(userCode);
        if(userCode.equals("")){
            System.out.println(111+"---------");
            resultMap.put("userCode","isNotNull");
        }else if(user == null){
            resultMap.put("userCode","inexistence");
        }else{
            resultMap.put("userCode","exist");
        }
        return  resultMap;
    }

    @RequestMapping("/jsp/getrolelist.do")
    @ResponseBody
    public List<Role> getRoleList(){
        return roleService.getRoleList();
    }

    @RequestMapping("/adduser.do")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/userlist.do";
    }

    @RequestMapping("/jsp/viewuser.do")
    public String viewuser(String method,int uid){
        User user = userService.getUserById(uid);
        session.setAttribute("user",user);
        if(method.equals("view")){
            return  "userview";
        }else {
            return "usermodify";
        }
    }



    @RequestMapping("/jsp/deluser.do")
    @ResponseBody
    public HashMap<String,String> deleteUser(String uid){
        userService.deleteUserById(uid);
        HashMap<String,String> resultMap = new HashMap<>();
        resultMap.put("delResult","true");
        return resultMap;
    }


    @RequestMapping("upduser.do")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/userlist.do";
    }



}
